#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include "Inc/server.h"
#include "Inc/types.h"

int main(int argc, char *argv[])
{

    // QGuiApplication app(argc, argv);
    // QQmlApplicationEngine engine;

    // // 使用资源系统加载QML文件
    // QString qmlPath = "qrc:/QtWidgetsApplication1/main.qml";
    // qDebug() << "尝试加载QML文件:" << qmlPath;

    // engine.load(QUrl(qmlPath));
    // // 检查是否加载成功

    // if (engine.rootObjects().isEmpty()) {
    //     qDebug() << "QML加载失败!";
    //     return -1;
    // }

    // return app.exec();

#ifdef __x86_64__
    qputenv("QT_QUICK_BACKEND", QByteArray("software"));
#else
    qputenv("QT_QPA_PLATFORM", QByteArray("eglfs"));
    system("killall weston");
    system("ip addr add 192.168.211.1/24 dev eth1");
    system("ip link set eth1 up");
    // system("ntpdate ntp.aliyun.com");
#endif

    qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));

#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    const QUrl url(QStringLiteral("qrc:/main.qml"));
    QObject::connect(
        &engine,
        &QQmlApplicationEngine::objectCreated,
        &app,
        [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        },
        Qt::QueuedConnection);

    qmlRegisterUncreatableMetaObject(
        Types::staticMetaObject,
        "MyApp", 1, 0,
        "Types",
        "Enum only"
    );

    // qmlRegisterType<Server>("Server", 1, 0, "Server");
    qmlRegisterType<Device>("Device", 1, 0, "Device");
    Server server;
    engine.rootContext()->setContextProperty("server", &server);

    engine.load(url);

    return app.exec(); 
}
